package com.bigdata.movieticketapi.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.common.utils.JwtUtil;
import com.bigdata.movieticketapi.entity.AdminUser;
import com.bigdata.movieticketapi.mapper.AdminUserMapper;
import com.bigdata.movieticketapi.service.IAdminUserService;
import com.bigdata.movieticketapi.vo.req.LoginReqVO;
import com.bigdata.movieticketapi.vo.req.PageUserReqVO;
import com.bigdata.movieticketapi.vo.req.RegisterReqVO;
import com.bigdata.movieticketapi.vo.resp.AdminIndexDataRespVO;
import com.bigdata.movieticketapi.vo.resp.LoginRespVO;
import com.bigdata.movieticketapi.vo.resp.PageUserRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-10-19
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {


    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private AdminUserMapper adminUserMapper;
    @Override
    public void register(RegisterReqVO vo) {
        AdminUser adminUser = query().select("id").last("limit 1").eq("username", vo.getUsername()).one();
        if(adminUser != null){
            throw new BusinessException(ResponseCode.ACCOUNT_ALREADY_EXISTS.getCode(),ResponseCode.ACCOUNT_ALREADY_EXISTS.getMessage());
        }
        AdminUser insertUser = new AdminUser();
        BeanUtils.copyProperties(vo,insertUser);
        insertUser.setPassword(SecureUtil.md5(vo.getPassword()));
        int result = adminUserMapper.insert(insertUser);
        if (result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public AdminIndexDataRespVO getIndexData() {
        return adminUserMapper.selectAdminIndexData();
    }

    @Override
    public LoginRespVO login(LoginReqVO vo) {
        AdminUser adminUser = query().select("password", "id").last("limit 1").eq("username", vo.getUsername()).one();
        if(adminUser==null){
            throw new BusinessException(ResponseCode.ACCOUNT_NOT_FOUND.getCode(), ResponseCode.ACCOUNT_NOT_FOUND.getMessage());
        }
        if (!SecureUtil.md5(vo.getPassword()).equals(adminUser.getPassword())){
            throw new BusinessException(ResponseCode.ACCOUNT_ERROR.getCode(), ResponseCode.ACCOUNT_ERROR.getMessage());
        }
        LoginRespVO result = new LoginRespVO();
        result.setId(adminUser.getId());
        result.setToken(jwtUtil.createToken(String.valueOf(adminUser.getId()),null));
        result.setUsername(vo.getUsername());
        return result;
    }

    @Override
    public PageResult<PageUserRespVO> pageUsers(PageUserReqVO vo) {
        IPage<PageUserRespVO> result = adminUserMapper.selectPageUser(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }
}
